const client = require('./index')
const {promisify} = require('util')
const promise_zscore = promisify(client.zscore).bind(client)
const promise_zadd = promisify(client.zadd).bind(client)
const promise_zincrby = promisify(client.zincrby).bind(client)
const promise_zrevrange = promisify(client.zrevrange).bind(client)
/**
 * 
 * @param {*} videoid 视频id
 * @param {*} cry 需要增加的分数
 */

const setHotc = async(videoid,crynum)=>{
    const data = await promise_zscore('hotvideos',videoid)
    if(data){
        //如果该记录存在 则添加相对应得分数
        await promise_zincrby('hotvideos',crynum,videoid)
    }else{
         //如果该记录不存在 则创建新的字段
         await promise_zadd('hotvideos',crynum,videoid)
    }
}
/**
 * 
 * @param {*} size 获取第几个的排名 
 */
const getTopHots = async (size)=>{
    const data = await promise_zrevrange('hotvideos',0,size - 1,'WITHSCORES')
    return data
}

module.exports = {
    setHotc,
    getTopHots
}